<?php
/**
 * Created by PhpStorm.
 * User: zxy
 * Date: 2018/2/28
 * Time: 10:18
 */
define("URL","http://www.maxun.xyz/controller/ScanController.php/?n=");
require_once  "../dao/UserDao.php";
require_once "../dao/CodeDao.php";
require_once  __DIR__."/../libs/phpqrcode.php";
class UserController
{
    function action(){
       $action =  $_GET["action"];
       if($action=="register"){
           $this->register();
       }else if($action=="login"){
           $this->login();
       }else if($action=="select"){
           $this->selectUserInfo();
       } else if($action=="toEdit"){
           $this->toEdit();
       }else if($action=="saveEdit"){
           $this->saveEdit();
       }else if($action=="selectCode"){
           $this->selectCode();
       }

    }
    function register(){
        $userDao = new UserDao();
        $id = uniqid();
        $data["id"] = $id;
        $data["nick_name"] = $_POST["nickname"];
        $data["really_name"] = $_POST["username"];
        $data["phone"] = $_POST["phone"];
        $data["email"] = $_POST["email"];
        $data["wechat"] = $_POST["wechat"];
        $data["qq"] = $_POST["qq"];
        $data["password"] =md5($_POST["password"]);
        $data["create_time"] = date("Y-m-d H:i:s",time());
        $row=  $userDao->insertUser($data);

        $codeDao = new CodeDao();
        $data1["id"] = uniqid();
        $data1["user_id"] = $id;
        $data1["code_id"] = $_POST["codeId"];
        $data1["create_time"] = date("Y-m-d H:i:s",time());
        $row1= $codeDao->insert($data1);

        $this->scerweima(URL.$_POST["codeId"],$_POST["codeId"]);
        if($row>0&&$row1>0){
            $array["message"] = "success";
            $array["id"] = $id;
            echo json_encode($array);
//            header("location:  ../../view/login.html?flag=1");
        }

    }

    function login(){
        $userDao = new UserDao();
        $codeDao = new CodeDao();
        $data =  $userDao->login($_POST["account"]);
        if(sizeof($data)>0){
            $account = $data[0]["password"];
            if($account==md5($_POST["password"])){
//            $_SESSION['$account'] = $account;
                $d["message"] ="success";
                $d["id"] =$data[0]["id"];
                $d["codeId"] = $_POST["codeId"];
                if($_POST["codeId"]!=""){
                    $code["id"] = uniqid();
                    $code["user_id"] = $data[0]["id"];
                    $code["code_id"] = $_POST["codeId"];
                    $code["create_time"] = date("Y-m-d H:i:s",time());
                    $codeDao->insert($code);
                    $this->scerweima(URL.$_POST["codeId"],$_POST["codeId"]);
                }
                echo json_encode($d);
            }else{
                $d["message"] ="fail";
                echo json_encode($d);
            }

        }else{
            $d["message"] ="fail";
            echo json_encode($d);
        }

    }

    function  selectUserInfo(){
        $userDao = new UserDao();
        $result = $userDao->slectUserById($_GET["id"]);
        echo json_encode($result);
    }
    function toEdit(){
        $userDao = new UserDao();
        $result = $userDao->slectUserById($_GET["id"]);
        echo json_encode($result);
    }

    function saveEdit(){
        $userDao = new UserDao();
        $data["nick_name"] =  $_GET["nickname"];
        $data["really_name"] =  $_GET["username"];
        $data["phone"] =  $_GET["phone"];
        $data["email"] =  $_GET["email"];
        $data["qq"] =  $_GET["qq"];
        if($_GET["password"]!="*****"){
            $data["password"] =  md5($_GET["password"]);
        }
       $num = $userDao->update($data,array('id = '=> $_GET["id"]));
       if($num!=0){
           $array["message"] = "success";
           echo json_encode($array);
       }else{
           $array["message"] = "fail";
           echo json_encode($array);
       }

    }

    function selectCode(){
        $codeDao = new CodeDao();
        echo json_encode($codeDao->selectAllCodesByUserId($_POST["id"]));
    }

    // 1. 生成原始的二维码(生成图片文件)
    function scerweima($url='',$codeId){
        $value = $url;                  //二维码内容
        $errorCorrectionLevel = 'L';    //容错级别
        $matrixPointSize = 5;           //生成图片大小

        //生成二维码图片
        $dir = iconv("UTF-8", "GBK", "../view/img/qrcode/".date("Y-m-d/",time()));
        if (!file_exists($dir)){
            mkdir ($dir,0777,true);
        }
        $filename = '../view/img/qrcode/'.date("Y-m-d/",time()).$codeId.'.png';
        QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);

        $QR = $filename;                //已经生成的原始二维码图片文件


        $QR = imagecreatefromstring(file_get_contents($QR));
        //输出图片
        imagepng($QR, '../view/img/qrcode.png');
        imagedestroy($QR);
        return '<img src="../view/img/qrcode.png" alt="扫一扫">';
    }



}

$obj = new UserController();
$obj->action();